<?php
class Scenery_reviews_model extends CI_Model
{
    private $_table   = 'scenery_reviews';
    private $_table_2 = 'scenery_reviews_reply';
    private $_table_3 = 'order_base';
    private $_table_4 = 'goods_base';
    private $_table_5 = 'scenery_info';
    
    public function total($params = array())
    {
        $this->db->from($this->_table . ' AS scenery_reviews');
        $this->db->join($this->_table_2 . ' AS scenery_reviews_reply', 'scenery_reviews.id = scenery_reviews_reply.reviews_id', 'LEFT');
        if (!empty($params['sid'])) {
            $this->db->where('scenery_reviews.sid', $params['sid']);
        } else {
            $this->db->where('scenery_reviews.sid', 0);
        }
        if (!empty($params['favourable'])) {
            $this->db->where('scenery_reviews.star >', $params['favourable']);
        }
        $this->db->where('scenery_reviews.status', 2);
        return $this->db->count_all_results();
    }
    
    public function page_list($page_num, $num, $params = array())
    {
        $this->db->select('scenery_reviews.*, scenery_reviews_reply.reply_user, scenery_reviews_reply.reply_content, scenery_reviews_reply.reply_time');
        $this->db->from($this->_table . ' AS scenery_reviews');
        $this->db->join($this->_table_2 . ' AS scenery_reviews_reply', 'scenery_reviews.id = scenery_reviews_reply.reviews_id', 'LEFT');
        if (!empty($params['sid'])) {
            $this->db->where('scenery_reviews.sid', $params['sid']);
        } else {
            $this->db->where('scenery_reviews.sid', 0);
        }
        $this->db->where('scenery_reviews.status', 2);
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    
    /**
     * 添加评论
     */
    public function insertReview($postData, $order)
    {
        $data = array(
            'order_id'  => $order->order_id,
            'sid'         => $order->sid,
            'goods_id'  => $order->goods_id,
            'uid'         => $postData['uid'],
            'user_name' => $postData['user_name'],
            'star'         => $postData['star'],
            'content'     => $postData['content'],
            'status'     => 1
        );
        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }

    /**
     * 根据订单id 来查询
     */
    public function findByOrderId($orderId)
    {
        $this->db->select('scenery_reviews.id, order_base.goods_id, order_base.order_id, order_base.goods_id, order_base.goods_name, order_base.sid');
        $this->db->from($this->_table. ' AS scenery_reviews');
        $this->db->join($this->_table_3. ' AS order_base', 'scenery_reviews.order_id=order_base.order_id', 'INNER');
        $this->db->where('scenery_reviews.order_id', $orderId);
        $this->db->limit(1);
        return $this->db->get();
    }
    
    /**
     * 计算好评率
     * @param array $params
     * @param string $state
     */
    public function favourableTotal($params = array(), $state = false)
    {
        $this->db->select('star, count(star) as star_count');
        $this->db->from($this->_table);
        if (!empty($params['sid'])) {
            $this->db->where('scenery_reviews.sid', $params['sid']);
        } else {
            $this->db->where('scenery_reviews.sid', 0);
        }
        $this->db->where('status', 2);
        $this->db->group_by('star');
        $result = $this->db->get();
        if ($state) {
            $rows = array();
            if ($result->num_rows() > 0) {
                foreach ($result->result() as $row) {
                    $rows[$row->star] = $row->star_count;
                }
            }
            return $rows;
        }
        return $result;
    }

    /**
     * 总页数
     */
    public function reviewTotal($params)
    {
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->where('uid', $params['uid']);
        $this->db->where('status', $params['status']); //审核通过
        return $this->db->count_all_results();
    }

    /**
     * 查找景区评论
     */
    public function queryReview($params, $start, $count)
    {
        $this->db->select('scenery_info.minipics, goods_name, star, content, status, scenery_reviews.created_at');
        $this->db->from($this->_table);
        $this->db->join($this->_table_4, 'scenery_reviews.goods_id= goods_base.goods_id', 'INNER');
        $this->db->join($this->_table_5, 'goods_base.sid = scenery_info.sid', 'LEFT');
        $this->db->where('uid', $params['uid']);
        $this->db->where('status', $params['status']); //审核通过
        $this->db->limit($count, $start);
        return $this->db->get();
    }
}